/*
 * TimeUtils.java
 *
 * Created on 8  2007, 10:23
 * Note: This source code is protected under the This software is licensed under the CC-GNU GPL.
 */
package OpenToday.Tools;

import OpenToday.Configuration.OpenTodayConfiguration;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * @author Menny Even Danan (c) 2007. Note: This source code is protected under the This software is licensed under the CC-GNU GPL.
 */
public class TimeUtils {

    public static long MS_PER_DAY = (24 * 60 * 60 * 1000);

    public static Date GetCurrentTime() {
        Date date = new Date();
        return CreateDate(date.getTime());
    }

    private static Date CreateDate(long timeInMS) {
        Date date = new Date();
        date.setTime(timeInMS);
        return date;
    }

    /**
     *  returns time zone offset and adds light saving time if required
     *  @return hours in MS
     **/
    public static long get_TimeZoneOffset() {
        //ExceptionUtils.PHASE("get_TimeZoneOffset");
        TimeZone here = TimeZone.getDefault();
        long offset = here.getRawOffset();
        if (here.useDaylightTime()) {
            offset += (60 * 60 * 1000);
        }
        //ExceptionUtils.PHASE("end get_TimeZoneOffset " + offset);
        return offset;
    }

    /**
     * Resturns a new Date object having correct time zoon and light saving time
     * @param date to be corrected
     * @return
     */
    public static Date NormalizeDate(Date date) {

        if (OpenTodayConfiguration.getInstance().get_EventsStoredInUTC())
            return new Date(date.getTime() + get_TimeZoneOffset());

        return date;
    }

    public static long NormalizeDate(long dateInMS) {

        if (OpenTodayConfiguration.getInstance().get_EventsStoredInUTC())
            dateInMS += get_TimeZoneOffset();

        return dateInMS;
    }

    private static String LeadingZero(int digit) {
        if (digit < 10) {
            return "0" + digit;
        } else {
            return "" + digit;
        }
    }

    public static String CreateSimpleTimeString(Date time) {
        // I did not understand the old solution
        // this is more readable
        Calendar c = Calendar.getInstance();
        //         the time should be normalized, Timezone and day light saving is added
        c.setTime( NormalizeDate(time) );

        String timeString = LeadingZero(c.get(Calendar.HOUR_OF_DAY))
                + ":" + LeadingZero(c.get(Calendar.MINUTE));

        return timeString;
    }

    public static long CalcualteRoundDate(long aDateInMS) {
        long daysCount = (aDateInMS / MS_PER_DAY);
        return MS_PER_DAY * daysCount;
    }

    public static Date CalcualteRoundDate(Date date) {
        return CreateRoundDate(date.getTime());
    }

    public static Date CreateRoundDate(long timeInMS) {
        long dateMS = CalcualteRoundDate(timeInMS);
        return CreateDate(dateMS);
    }

    public static long CalculateDaysDiff(long currentDay, long futureEventTime) {
        long diff = futureEventTime - currentDay;
        return diff / MS_PER_DAY;
    }

    /**
     * set HOUR_OF_DAY, hour, minute, seconde and millisecond of a time to 0
     * @param timeAsDate current time
     * @return 
     */
    public static Date getDayStart(Date timeAsDate) {

        Calendar c = Calendar.getInstance();
        c.setTime(timeAsDate);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    /**
     * set hour, minute, seconde and millisecond of a time to 0
     * @param timeInMS current time in MS
     * @return
     */
    public static long getDayStart(long timeInMS) {

        return getDayStart(new Date(timeInMS)).getTime();
    }

    /**
     * set HOUR_OF_DAY 23, hour 23, minute 59, seconde 59 and millisecond 999
     * might not work, because you like to have 00:00 if the next day
     * better add MS_PER_DAY = (24 * 60 * 60 * 1000) to TimeUtils.getDayStart
     * @param timeAsDate current time
     * @return
     */
    public static Date getDayEnd(Date timeAsDate) {

        Calendar c = Calendar.getInstance();
        c.setTime(timeAsDate);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.HOUR, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);

        return c.getTime();
    }

    /**
     * @see Date getDayEnd
     * @param timeInMS current time in MS
     * @return
     */
    public static long getDayEnd(long timeInMS) {

        return getDayStart(new Date(timeInMS)).getTime();
    }
}
